package routers

import (
	"fmt"
	"github.com/couchbase/gocb/v2"
	"log"
	"time"
)

func Aa() {
	//connectionString := "192.168.31.49:8091/"
	connectionString := "192.168.31.49"
	bucketName := "goDemo"
	username := "Administrator"
	password := "Syzh@123!"

	// For a secure cluster connection, use `couchbases://<your-cluster-ip>` instead.
	cluster, err := gocb.Connect("couchbase://"+connectionString, gocb.ClusterOptions{
		Authenticator: gocb.PasswordAuthenticator{
			Username: username,
			Password: password,
		},
	})
	if err != nil {
		log.Fatal(err)
	}

	bucket := cluster.Bucket(bucketName)
	err = bucket.WaitUntilReady(5*time.Second, nil)
	if err != nil {
		log.Fatal(err)
	}

	col := bucket.Scope("tenant_agent_00").Collection("users")

	type User struct {
		Name      string   `json:"name"`
		Email     string   `json:"email"`
		Interests []string `json:"interests"`
	}

	// Create and store a Document
	_, err = col.Upsert("u:jade",
		User{
			Name:      "Jade",
			Email:     "jade@test-email.com",
			Interests: []string{"Swimming", "Rowing"},
		}, nil)
	if err != nil {
		log.Fatal(err)
	}

	// Get the document back
	getResult, err := col.Get("u:jade", nil)
	if err != nil {
		log.Fatal(err)
	}

	//var inUser2 User
	//db_test.ReplyList()

	var inUser User
	err = getResult.Content(&inUser)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("User: %v\n", inUser)

	// Perform a N1QL Query
	//inventoryScope := bucket.Scope("inventory")
	//queryResult, err := inventoryScope.Query(
	//	fmt.Sprintf("SELECT * FROM goDemo WHERE id=10"),
	//	&gocb.QueryOptions{Adhoc: true},
	//)
	//if err != nil {
	//	log.Fatal(err)
	//}
	//
	//// Print each found Row
	//for queryResult.Next() {
	//	var result interface{}
	//	err := queryResult.Row(&result)
	//	if err != nil {
	//		log.Fatal(err)
	//	}
	//	fmt.Println(result)
	//}
	//
	//if err := queryResult.Err(); err != nil {
	//	log.Fatal(err)
	//}

}

// https://docs.couchbase.com/go-sdk/current/howtos/n1ql-queries-with-sdk.html
func CreateScopeTest() {
	connectionString := "192.168.31.49"
	//bucketName := "goDemo"
	username := "Administrator"
	password := "Syzh@123!"

	// For a secure cluster connection, use `couchbases://<your-cluster-ip>` instead.
	cluster, err := gocb.Connect("couchbase://"+connectionString, gocb.ClusterOptions{
		Authenticator: gocb.PasswordAuthenticator{
			Username: username,
			Password: password,
		},
	})
	if err != nil {
		log.Fatal(err)
	}

	_, err = cluster.Query("CREATE SCOPE `goDemo`.test", &gocb.QueryOptions{
		// Note that we set Adhoc to true to prevent this query being run as a prepared statement.
		Adhoc: true,
	})
	_, err = cluster.Query("CREATE SCOPE `goDemo`.test2", &gocb.QueryOptions{
		// Note that we set Adhoc to true to prevent this query being run as a prepared statement.
		Adhoc: true,
	})
	if err != nil {
		log.Fatal(err)
	}

	//bucket := cluster.Bucket(bucketName)
	//err = bucket.WaitUntilReady(5*time.Second, nil)
	//if err != nil {
	//	log.Fatal(err)
	//}
	//err = gocb.CollectionManager.CreateScope("test", &gocb.CreateScopeOptions{})
	//if err != nil {
	//	log.Fatal(err)
	//}

}
